package com.css.zfzx.sjcj.modules.drill.basedivision.basedivision.service.Impl;


import com.css.zfzx.sjcj.modules.drill.basedivision.basedivision.repository.BaseDivisionRepository;
import com.css.zfzx.sjcj.modules.drill.basedivision.basedivision.repository.entity.DivisionsEntity;
import com.css.zfzx.sjcj.modules.drill.basedivision.basedivision.service.BaseDivisionService;
import com.css.zfzx.sjcj.modules.drill.basedivision.basedivision.vo.DivisionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class BaseDivisionServiceImpl implements BaseDivisionService {
   @Autowired
   BaseDivisionRepository baseDivisionRepository;
    @Override
    public String findDivisionCodeByName(DivisionVO divisionVO, String type) {
        if("1".equals(type)) {
            String proviceCode="";
            if(!StringUtils.isEmpty(divisionVO.getProvince())) {
                List<DivisionsEntity> validDivisionPro = baseDivisionRepository.findValidDivisionCodeByName(divisionVO.getProvince());
                for (DivisionsEntity entity : validDivisionPro) {
                    proviceCode=entity.getDivisionCode();
                }
            }
            return proviceCode;
        }else if("2".equals(type)) {
            String cityCode="";
            if(!StringUtils.isEmpty(divisionVO.getCity())) {
                List<DivisionsEntity> divisionCodeCity = baseDivisionRepository.findValidDivisionCodeByName(divisionVO.getCity());
                if (divisionCodeCity.size() == 1) {
                    for (DivisionsEntity entity : divisionCodeCity) {
                        cityCode=entity.getDivisionCode();
                    }
                    return cityCode;
                }else {
                    for (DivisionsEntity entity : divisionCodeCity) {
                        DivisionsEntity divisionsEntity = baseDivisionRepository.findById(entity.getParentId()).get();
                        if(divisionsEntity.getDivisionName().equals(divisionVO.getProvince())){
                            return entity.getDivisionCode();
                        }
                    }
                }
            }
        }else if("3".equals(type)) {
            String areaCode="";
            if(!StringUtils.isEmpty(divisionVO.getArea())) {
                List<DivisionsEntity> divisionCodeArea = baseDivisionRepository.findValidDivisionCodeByName(divisionVO.getArea());
                if (divisionCodeArea.size() == 1) {
                    for (DivisionsEntity entity : divisionCodeArea) {
                        areaCode = entity.getDivisionCode();
                    }
                    return areaCode;
                }else{
                    for (DivisionsEntity entity : divisionCodeArea) {
                        DivisionsEntity divisionsEntity = baseDivisionRepository.findById(entity.getParentId()).get();
                        if(divisionsEntity.getDivisionName().equals(divisionVO.getCity())){
                            return entity.getDivisionCode();
                        }
                    }
                }
            }
        }
        return "";
    }
}
